package cn.iocoder.yudao.module.crossborder.order.controller.admin.ordershipment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.order.controller.admin.ordershipment.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.ordershipment.OrderShipmentDO;
import cn.iocoder.yudao.module.crossborder.order.service.ordershipment.OrderShipmentService;

@Tag(name = "管理后台 - 跨境电商订单发货")
@RestController
@RequestMapping("/crossborder/order-shipment")
@Validated
public class OrderShipmentController {

    @Resource
    private OrderShipmentService orderShipmentService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商订单发货")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment:create')")
    public CommonResult<Long> createOrderShipment(@Valid @RequestBody OrderShipmentSaveReqVO createReqVO) {
        return success(orderShipmentService.createOrderShipment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商订单发货")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment:update')")
    public CommonResult<Boolean> updateOrderShipment(@Valid @RequestBody OrderShipmentSaveReqVO updateReqVO) {
        orderShipmentService.updateOrderShipment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商订单发货")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment:delete')")
    public CommonResult<Boolean> deleteOrderShipment(@RequestParam("id") Long id) {
        orderShipmentService.deleteOrderShipment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商订单发货")
                @PreAuthorize("@ss.hasPermission('crossborder:order-shipment:delete')")
    public CommonResult<Boolean> deleteOrderShipmentList(@RequestParam("ids") List<Long> ids) {
        orderShipmentService.deleteOrderShipmentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商订单发货")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment:query')")
    public CommonResult<OrderShipmentRespVO> getOrderShipment(@RequestParam("id") Long id) {
        OrderShipmentDO orderShipment = orderShipmentService.getOrderShipment(id);
        return success(BeanUtils.toBean(orderShipment, OrderShipmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商订单发货分页")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment:query')")
    public CommonResult<PageResult<OrderShipmentRespVO>> getOrderShipmentPage(@Valid OrderShipmentPageReqVO pageReqVO) {
        PageResult<OrderShipmentDO> pageResult = orderShipmentService.getOrderShipmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderShipmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商订单发货 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:order-shipment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderShipmentExcel(@Valid OrderShipmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderShipmentDO> list = orderShipmentService.getOrderShipmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商订单发货.xls", "数据", OrderShipmentRespVO.class,
                        BeanUtils.toBean(list, OrderShipmentRespVO.class));
    }

}